package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.module.admin.entity.SysSchool;
import com.mars.module.admin.mapper.SysSchoolMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysMajorRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysMajorMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysMajor;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysMajorService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 专业业务层处理
 *
 * @author mars
 * @date 2024-03-14
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysMajorServiceImpl implements ISysMajorService {

    private final SysMajorMapper baseMapper;
    private final SysSchoolMapper schoolMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysMajor add(SysMajorRequest request) {
        SysMajor entity = SysMajor.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysMajorRequest request) {
        SysMajor entity = SysMajor.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysMajor getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysMajor> pageList(SysMajorRequest request) {
        Page<SysMajor> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysMajor> query = this.buildWrapper(request);
        IPage<SysMajor> pageRecord = baseMapper.selectPage(page, query);
        List<SysMajor> records = pageRecord.getRecords();
        List<SysMajor> recordsNew = new ArrayList<>();
        for (SysMajor e : records) {
            SysSchool sysSchool = schoolMapper.selectById(e.getSchoolId());
            e.setSchoolName(sysSchool == null ? "" : sysSchool.getName());
            recordsNew.add(e);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysMajor> list(SysMajorRequest request) {
        LambdaQueryWrapper<SysMajor> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysMajor> buildWrapper(SysMajorRequest param) {
        LambdaQueryWrapper<SysMajor> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysMajor::getName, param.getName());
        }
        if (param.getSchoolId() != null) {
            query.like(SysMajor::getSchoolId, param.getSchoolId());
        }
        if (StringUtils.isNotBlank(param.getTeacher())) {
            query.like(SysMajor::getTeacher, param.getTeacher());
        }
        return query;
    }

}
